﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using WMPLib;

using MusicPlayerLib.ViewModel;
using MusicPlayerLib.Utils;
using MusicPlayerLib.DataModel;


namespace MusicPlayerLib
{
    public class MusicPlayer
    {
        #region Filed
        //播放列表;
        private PlaylistViewModel playlist;
        private bool isPaused;
        private bool isMute;
        private WindowsMediaPlayer myplayer;
        private int volume = 0;
        //当前播放的曲目的地址;
        private string url;
        //播放器的更新线程，用于更新播放的时间以及在播放完成之后切换到下一首歌曲
        private BackgroundWorker bgWorker;

        public event DoWorkEventHandler UpdatePlayedTime;
        #endregion //Filed

        #region Constructor
        public MusicPlayer()
        {
            myplayer = new WindowsMediaPlayer();
            playlist = new PlaylistViewModel();
            this.volume = myplayer.settings.volume;
            isPaused = true;
        }

        public MusicPlayer(PlaylistViewModel playlist)
        {
            myplayer = new WindowsMediaPlayer();
            this.playlist = playlist;
            this.volume = myplayer.settings.volume;
            url = playlist.GetCurrentTrack().Location;
            isPaused = true;
        }

        #endregion //Constructor

        #region Methods
        public void StartPlayback()   //从头开始播放
        {
            //创建一个更新播放时间的线程;
            //if (bgWorker != null)
            //{
            //    bgWorker.Dispose();
            //}

            bgWorker = new BackgroundWorker();
            bgWorker.DoWork += UpdatePlayedTime;
            bgWorker.RunWorkerAsync();
            url = playlist.GetCurrentTrack().Location;

            myplayer.URL = url;
            myplayer.controls.play();
        }
        
        public void PausePlayback()   //暂停
        {
            try
            {
                myplayer.controls.pause();
                isPaused = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        
        public void ResumePlayback()  //恢复暂停的播放
        {
            if (isPaused)
            {
                myplayer.controls.play();
                isPaused = false;
            }
        }

        public void SetVal(int value)     //设置声音
        {
            if (value < 0 || value > 100)
                return;
            myplayer.settings.volume = value;
            this.volume = value;
        }

        public void Mute()        //静音
        {
            if (isMute)
                return;
            //保存当前音量;
            volume = myplayer.settings.volume;
            myplayer.settings.volume = 0;
            isMute = true; 
        }

        public void ReMute()         //恢复静音
        {
            if (isMute)
            {
                myplayer.settings.volume = volume;
                isMute = false;
            }
        }

        public void StopPlayback()    //停止；
        {
            myplayer.controls.stop();
        }
        
        public void SeekTo(double position)          //从给定的位置播放
        {
            try
            {
                myplayer.controls.currentPosition = position;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }

        public void PlayPreviousTrack()
        {
            playlist.MoveToPrevious();

            AudioFile af = playlist.GetCurrentTrack();
            //停止播放当前曲目;
            url = af.Location;
            StartPlayback();
        }

        public void PlayNextTrack()
        {
            playlist.MoveToNext();

            AudioFile af = playlist.GetCurrentTrack();
            //停止播放当前曲目;
            url = af.Location;
            StartPlayback();
        }

        public double GetPosition()
        {
            return myplayer.controls.currentPosition;

        }
        #endregion

       

        #region Property
        public bool IsPaused
        {
            get { return isPaused; }
            set
            {
                isPaused = value;
            }
        }
        

        public bool IsMute
        {
            get { return isMute; }
        }
        #endregion //Property
    }
}
